<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="主键" prop="orgId">
        <el-input
          v-model="queryParams.orgId"
          placeholder="请输入主键"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
      
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
         
        >删除</el-button>
      </el-col>
      <el-col :span="1.5" v-show="false">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
         
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orgSecondList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="orgId" />
      <el-table-column label="级别" align="center" prop="labRankOpt" :formatter="labRankOptFormat" />
      <el-table-column label="建筑面积" align="center" prop="buildAreaSum" />
      <el-table-column label="网址" align="center" prop="webSite" />
      <el-table-column label="类型" align="center" prop="labTypeOpt" :formatter="labTypeOptFormat" />
      <el-table-column label="使用面积" align="center" prop="usingArea" />
      <el-table-column label="实验室数" align="center" prop="labNumSum" />
      <el-table-column label="工作人数" align="center" prop="employeeNumSum" />
      <el-table-column label="实验平台" align="center" prop="labPlatformOpt" :formatter="labPlatformOptFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
           
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改二级机构信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="级别" prop="labRankOpt">
          <el-select v-model="form.labRankOpt" placeholder="请选择级别">
            <el-option
              v-for="dict in labRankOptOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="建筑面积" prop="buildAreaSum">
          <el-input v-model="form.buildAreaSum" placeholder="请输入建筑面积" />
        </el-form-item>
        <el-form-item label="网址" prop="webSite">
          <el-input v-model="form.webSite" placeholder="请输入网址" />
        </el-form-item>
        <el-form-item label="类型" prop="labTypeOpt">
          <el-select v-model="form.labTypeOpt" placeholder="请选择类型">
            <el-option
              v-for="dict in labTypeOptOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="使用面积" prop="usingArea">
          <el-input v-model="form.usingArea" placeholder="请输入使用面积" />
        </el-form-item>
        <el-form-item label="实验室数" prop="labNumSum">
          <el-input v-model="form.labNumSum" placeholder="请输入实验室数" />
        </el-form-item>
        <el-form-item label="工作人数" prop="employeeNumSum">
          <el-input v-model="form.employeeNumSum" placeholder="请输入工作人数" />
        </el-form-item>
        <el-form-item label="实验平台" prop="labPlatformOpt">
          <el-select v-model="form.labPlatformOpt" placeholder="请选择实验平台">
            <el-option
              v-for="dict in labPlatformOptOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrgSecond, getOrgSecond, delOrgSecond, addOrgSecond, updateOrgSecond, exportOrgSecond } from "@/api/labdata/orgSecond";

export default {
  name: "OrgSecond",
  data() {
    return {
      isNew : true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 二级机构信息表格数据
      orgSecondList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 级别字典
      labRankOptOptions: [],
      // 类型字典
      labTypeOptOptions: [],
      // 实验平台字典
      labPlatformOptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orgId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        labRankOpt: [
          { required: true, message: "级别不能为空", trigger: "change" }
        ],
        labTypeOpt: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        labPlatformOpt: [
          { required: true, message: "实验平台不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("labRankOpt ").then(response => {
      this.labRankOptOptions = response.data;
    });
    this.getDicts("labTypeOpt").then(response => {
      this.labTypeOptOptions = response.data;
    });
    this.getDicts("labPlatformOpt").then(response => {
      this.labPlatformOptOptions = response.data;
    });
  },
  methods: {
    /** 查询二级机构信息列表 */
    getList() {
      this.loading = true;
      listOrgSecond(this.queryParams).then(response => {
        this.orgSecondList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 级别字典翻译
    labRankOptFormat(row, column) {
      return this.selectDictLabel(this.labRankOptOptions, row.labRankOpt);
    },
    // 类型字典翻译
    labTypeOptFormat(row, column) {
      return this.selectDictLabel(this.labTypeOptOptions, row.labTypeOpt);
    },
    // 实验平台字典翻译
    labPlatformOptFormat(row, column) {
      return this.selectDictLabel(this.labPlatformOptOptions, row.labPlatformOpt);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orgId: null,
        labRankOpt: null,
        buildAreaSum: null,
        webSite: null,
        labTypeOpt: null,
        usingArea: null,
        labNumSum: null,
        employeeNumSum: null,
        labPlatformOpt: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orgId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.orgId=5;
      this.open = true;
      this.title = "添加二级机构信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orgId = row.orgId || this.ids
      getOrgSecond(orgId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改二级机构信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (!this.isNew) {
            updateOrgSecond(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrgSecond(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orgIds = row.orgId || this.ids;
      this.$confirm('是否确认删除二级机构信息编号为"' + orgIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delOrgSecond(orgIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有二级机构信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportOrgSecond(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
